package com.xinjie.converter;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// 将String类型的日期转成Date类型
public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        DateFormat dateFormat = null;

        try {
            // 输入的日期为空时
            if (StringUtils.isEmpty(source)) {
                throw new NullPointerException("请输入要转换的日期！");
            }
            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(source);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException("日期输入有误！");
        }
    }
}
